﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XDL.Framework.Graphics
{
    class GradientTexture : 
        Texture2D        
    {
        /// <summary>
        /// Creates new gradient texture
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="h">Height of the texture</param>
        public GradientTexture(GraphicsDevice graphicsDevice, int h):
            base(graphicsDevice,1,h,true,SurfaceFormat.Color)
        {
        }

        /// <summary>
        /// Draw the gradient texture in the given SpriteBatch.
        /// </summary>
        /// <remarks>
        /// The method must be called between a SpriteBatch.Begin()/End()
        /// </remarks>
        /// <param name="spriteBatch">Sprite batch</param>
        /// <param name="rectangle">Destination rectangle</param>
        /// <param name="color">Base color</param>
        public void Draw(SpriteBatch spriteBatch,Rectangle rectangle,Color color)
        {
            spriteBatch.Draw(this, rectangle, Color.White);
        }
 
        /// <summary>
        /// Fill the texture with the given colors.
        /// </summary>
        /// <param name="startColor">Start color (top)</param>
        /// <param name="endColor">End color (bottom)</param>
        public void Fill(Color startColor, Color endColor)
        {
            Vector4 tmpStart = startColor.ToVector4();
            Vector4 tmpEnd = endColor.ToVector4();

            int h = this.Height;
            Color[] colorGradient = new Color[h];
            for (int i = 0; i < h; i++)
            {
                float ratio = i / (float)h;

                colorGradient[i] = new Color(Vector4.Lerp(tmpStart, tmpEnd, ratio));
            }
            // copy the colors
            this.SetData<Color>(colorGradient);
        }

        /// <summary>
        /// Fills the texture with a unique color.
        /// </summary>
        /// <param name="fillColor">Fill color.</param>
        public void Fill(Color fillColor)
        {
            int h = this.Height;
            Color[] colorGradient = new Color[h];
            for (int i = 0; i < h; i++)
                colorGradient[i] = fillColor;
            // copy the colors
            this.SetData<Color>(colorGradient);
        }
    }
}
